package com.cy.mall.web.front.wj;

import com.cy.mall.model.Result;
import com.cy.mall.model.ao.wj.WjOrderAddAO;
import com.cy.mall.model.ao.wj.WjOrderEditAO;
import com.cy.mall.model.domain.wj.WjOrder;
import com.cy.mall.model.dto.bigdata.BigDataCheckParamDTO;
import com.cy.mall.model.dto.customer.ValidateBigDataResultDTO;
import com.cy.mall.model.enums.CheckResult;
import com.cy.mall.model.enums.ResultCode;
import com.cy.mall.model.vo.wj.WjOrderDetail;
import com.cy.mall.service.comm.BigDataService;
import com.cy.mall.service.customer.CustomerService;
import com.cy.mall.service.wj.WjOrderService;
import com.cy.mall.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 前台-万家分期订单管理
 * @Author tanyilin
 * @Date 2019/5/10 16:35
 */
@RestController
@RequestMapping("/customer/wj/order")
@Api(tags = "前台-万家分期订单管理")
public class WjOrderController extends BaseController {

    @Autowired
    private WjOrderService  wjOrderService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private BigDataService  bigDataService;

    @ApiOperation("新增订单")
    @PostMapping("/do/add")
    public Result doAddWjOrder(@Validated WjOrderAddAO addAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        Long customerId = this.getLoginCustomer().getCustomerId();
        //新增订单
        Result addWjOrderResult = wjOrderService.addWjOrder(addAO, customerId);
        Long wjOrderId = (Long) addWjOrderResult.getData();
        Map<String, Object> finalResultDataMap = new HashMap<>();
        finalResultDataMap.put("wjOrderId", wjOrderId);

        //查询客户大数据是否通过
        ValidateBigDataResultDTO validateBigDataResultDTO = customerService.validateBigDataResult(customerId);
        //情况1：如果大数据查询过了，并且结果=不通过
        if(CheckResult.NOT_PASS.equals(validateBigDataResultDTO.getBigDataCheckResult())) {

            //更新订单备注信息：大数据审核不通过
            WjOrder updateWjOrder = new WjOrder();
            updateWjOrder.setId(wjOrderId);
            updateWjOrder.setRemark("大数据审核不通过，失败代码：".concat(validateBigDataResultDTO.getBlackFactsCode()));
            wjOrderService.updateWjOrder(updateWjOrder);

            finalResultDataMap.put("blackFactsCode", validateBigDataResultDTO.getBlackFactsCode());
            return ok("新增订单成功，但客户大数据查询结果不通过", ResultCode.ADD_ORDER_SUCCESS_BIGDATA_FAIL, finalResultDataMap);
        }
        //情况2：如果未check,就去check
        if(CheckResult.UNCHECKED.equals(validateBigDataResultDTO.getBigDataCheckResult())) {

            //封装查询参数
            BigDataCheckParamDTO paramDTO = new BigDataCheckParamDTO();
            paramDTO.setName(validateBigDataResultDTO.getCustomerName());
            paramDTO.setIdCard(validateBigDataResultDTO.getIdNo());
            paramDTO.setMobile(validateBigDataResultDTO.getMobile());
            //查询客户大数据
            Result bigDataCheckResult = bigDataService.check(paramDTO);
            if(!bigDataCheckResult.isOk()) {
                //请求大数据查询失败，但是还是返回保存新增订单成功的信息
                return ok("新增订单成功", finalResultDataMap);
            }

            //请求大数据查询成功，根据风控规则判断客户是否通过，并更新客户大数据查询结果信息表
            Map<String, Object> dataMap = (Map) bigDataCheckResult.getData();
            Result judgeResult = customerService.judgeBigDataCheckResult(dataMap, customerId);
            if(!judgeResult.isOk()) {

                //更新订单备注信息：大数据审核不通过
                WjOrder updateWjOrder = new WjOrder();
                updateWjOrder.setId(wjOrderId);
                updateWjOrder.setRemark("大数据审核不通过，失败代码：".concat(String.valueOf(judgeResult.getData())));
                wjOrderService.updateWjOrder(updateWjOrder);

                finalResultDataMap.put("blackFactsCode", String.valueOf(judgeResult.getData()));
                return ok("新增订单成功，但客户大数据查询结果判定为：不通过", ResultCode.ADD_ORDER_SUCCESS_BIGDATA_FAIL, finalResultDataMap);
            }
        }

        return ok("新增订单成功，且大数据通过", finalResultDataMap);
    }

    @ApiOperation("保存编辑订单")
    @PostMapping("/do/edit")
    public Result doEditWjOrder(@Validated WjOrderEditAO editAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        Map<String, Object> finalResultDataMap = new HashMap<>();
        finalResultDataMap.put("wjOrderId", editAO.getWjOrderId());

        //查询客户大数据是否通过
        Long customerId = this.getLoginCustomer().getCustomerId();
        ValidateBigDataResultDTO validateBigDataResultDTO = customerService.validateBigDataResult(customerId);
        if(CheckResult.NOT_PASS.equals(validateBigDataResultDTO.getBigDataCheckResult())) {
            finalResultDataMap.put("blackFactsCode", validateBigDataResultDTO.getBlackFactsCode());
            return ok("", ResultCode.ADD_ORDER_SUCCESS_BIGDATA_FAIL, finalResultDataMap);
        }

        //保存编辑
        WjOrder updateWjOrder = new WjOrder();
        BeanUtils.copyProperties(editAO, updateWjOrder);
        updateWjOrder.setId(editAO.getWjOrderId());
        wjOrderService.updateWjOrder(updateWjOrder);

        return ok("保存编辑订单成功，且大数据通过", finalResultDataMap);
    }

    @ApiOperation("客户最新的一条万家分期询单信息")
    @GetMapping("/get/last/wjOrderDetail")
    public Result<WjOrderDetail> getLastWjOrderDetail() {
        WjOrderDetail wjOrderDetail = wjOrderService.getLastWjOrderDetail(this.getLoginCustomer().getCustomerId());
        return ok("", wjOrderDetail);
    }

}
